#include "cards.h"


Cards::Cards( Card &card)
{
    this->add(card);
}

void Cards::add(Card &card)
{
    this->m_cards.insert(card);
}

void Cards::add(const Cards &cards)
{
    this->m_cards.unite(cards.m_cards);
}


Cards &Cards::operator<<(Card &card)
{
    add(card);
    return *this;

}

Cards &Cards::operator<<(const Cards &cards)
{
    add(cards);
    return *this;
}

void Cards::remove(Card &card)
{
    this->m_cards.remove(card);
}

void Cards::remove(Cards &cards)
{
    this->m_cards.subtract(cards.m_cards);
}

int Cards::CardCount()
{
    return this->m_cards.size();
}

bool Cards::isEmpty()
{
    return this->m_cards.size()==0;
}

void Cards::clear()
{
    this->m_cards.clear();
}

Card::CardPoint Cards::maxPoint()
{
    Card::CardPoint max=Card::Card_Begin;
    if(!m_cards.isEmpty())
    {
        for(auto it=m_cards.begin();it!=m_cards.end();++it)
        {
            if(it->getPoint()>max)
            {
                max=it->getPoint();
            }
        }
    }
    return max;
}

Card::CardPoint Cards::minPoint()
{
    Card::CardPoint min=Card::Card_End;
    if(!m_cards.isEmpty())
    {
        for(auto it=m_cards.begin();it!=m_cards.end();++it)
        {
            if(it->getPoint()<min)
            {
                min=it->getPoint();
            }
        }
    }
    return min;
}

int Cards::pointCount(Card::CardPoint point)
{
    int count=0;
    for(auto it=m_cards.begin();it!=m_cards.end();it++)
    {
        if(it->getPoint()==point)
        {
            count++;
        }
    }
    return count;
}

bool Cards::contains(const Card &card)
{
    return m_cards.contains(card);
}

bool Cards::contains(const Cards &cards)
{
    return m_cards.contains(cards.m_cards);
}

Card Cards::takeRandCard()
{
    //生成一个随机数
    qsrand(QTime::currentTime().msec());
    std::srand(std::time(nullptr));
    int randomNumber = std::rand() % m_cards.size();
    qDebug()<<randomNumber;
    QSet<Card>::const_iterator it=m_cards.constBegin();
    for(int i=0;i<randomNumber;i++,it++);
    Card card=*it;
    m_cards.erase(it);
    return card;
}

CardList Cards::toCardList(Cards::SortType type)
{
    CardList list;
    for(auto it=m_cards.begin();it!=m_cards.end();it++)
    {
        list<<*it;
    }
    if(type==Asc)
    {
       std::sort(list.begin(),list.end(),lessSort);
    }
    else if(type==Desc)
    {
         std::sort(list.begin(),list.end(),greateSort);
    }
    return list;
}

Cards::Cards()
{

}
